using System.Collections.Generic;
using BasicScript.Component;
using Game.Component;
using Game.Core;
using Game.Core.Loader;
using Game.Main.Event;
using Game.Utils;
using Game.XH;
using UnityEngine;

namespace Game.Main.View
{
	public class StorageBox : EventObject
	{
		public static StorageBox Instance;
		private const float CHARACTER_HEIGHT = 3f;

		private const float CHARACTER_GAP = 0.4f;

		private const float EXTEND_HEIGHT = 3f;

		public Transform storageBox;

		public Transform floor;

		public Transform box;

		public Transform itemTrans;

		public float difValX;

		private float screenHalfWidth;

		private float screenHeight = 10.24f;

		private float boxHeight;

		private float boxStaticMinY;

		private float boxStaticMaxY;

		private float boxMoveMinY;

		private float boxMoveMaxY;

		private float itemStartY;

		private float itemEndBoxY;

		private List<CharacterItem> items = new List<CharacterItem>();

		private bool isOpen;

		private float waitTimer;

		private float animTimer;

		private Transform btnTrans;

		private GameObject lockMask;

		private GameObject btn;

		private Collider2D floorColl;

		private FrameAnimation btnAnim;

		public override void Init()
		{
			ResourceLoader resourceLoader = new ResourceLoader("prefabs/map/storagebox", "storagebox.prefab");
			resourceLoader.Bind(LoadEvent.LOAD_COMPLETE, OnResourceLoadComplete);
			resourceLoader.Load();
			Instance = this;
			//IsLock(false);

        }

		public void LatedUpdate()
		{
			if (isOpen)
			{
				return;
			}
			if (waitTimer >= 5f)
			{
				animTimer += Time.deltaTime * 4f;
				if (animTimer > 16f)
				{
					btnTrans.localEulerAngles = new Vector3(0f, 0f, MathUtil.GetCurveValue(animTimer) * 5f);
					if (animTimer > 24f)
					{
						animTimer = 0f;
					}
				}
			}
			else
			{
				waitTimer += Time.deltaTime;
			}
		}

		private void OnResourceLoadComplete(BaseEvent evt)
		{
			ResourceLoader resourceLoader = evt.sender as ResourceLoader;
			storageBox = Object.Instantiate((GameObject)resourceLoader.resource).transform;
			storageBox.name = "StorageBox";
			storageBox.position = new Vector3(0f, 0f, -1f);
			floor = storageBox.Find("Floor");
			box = floor.Find("Anchor/Box");
			itemTrans = box.Find("Item");
			itemTrans.SetLocalZ(-0.1f);
			lockMask = floor.Find("Anchor/LockMask").gameObject;
			floorColl = floor.GetComponent<BoxCollider2D>();
			SetFloorColliderEnabled(false);
			btnTrans = storageBox.Find("Btn");
			btn = btnTrans.Find("Open").gameObject;
			btnAnim = btn.GetComponent<FrameAnimation>();
			Fire(new BaseEvent(LoadEvent.LOAD_COMPLETE));
		}

		public void IsLock(bool val)
		{
			lockMask.SetActive(val);
			for (int i = 0; i < items.Count; i++)
			{
				CharacterItem characterItem = items[i];
				if (characterItem != null)
				{
					characterItem.IsLock(val);
				}
			}
		}

		public void SetScreenWidth(float halfWidth)
		{
			screenHalfWidth = halfWidth;
		}

		public void SetPositionX(float x)
		{
			storageBox.position = new Vector3(x + screenHalfWidth - difValX, storageBox.position.y, storageBox.position.z);
		}

		public void InitStorageBoxItems()
		{
			items.Clear();
			boxHeight = 0f;
			int childCount = itemTrans.childCount;
			for (int i = 0; i < childCount; i++)
			{
				Transform child = itemTrans.GetChild(i);
				BaseItem component = child.GetComponent<BaseItem>();
				if (component != null && component.itemData.scriptType == 1)
				{
					items.Add((CharacterItem)component);
					boxHeight += ((CharacterItem)component).characterHeight + 0.4f;
				}
			}
			SortItemsByY();
			UpdateStorageBoxItemPosition(true);
			SetFloorVisible(false);
		}

		public void IsOpen(bool val)
		{
			if (val)
			{
				btn.name = "Close";
				btnAnim.Play();
			}
			else
			{
				btn.name = "Open";
				btnAnim.PlayReversed();
			}
			isOpen = val;
			waitTimer = 0f;
			animTimer = 0f;
			btnTrans.localEulerAngles = Vector3.zero;
		}

		public void UpdateStorageBoxItemPosition(bool needUpdatePosition, BaseItem newItem = null)
		{
			itemEndBoxY = boxHeight / 2f;
			itemStartY = 0f - itemEndBoxY;
			if (boxHeight <= screenHeight)
			{
				boxMoveMaxY = 3f;
				boxStaticMaxY = 0f;
			}
			else
			{
				boxStaticMaxY = (boxHeight - screenHeight) / 2f;
				boxMoveMaxY = boxStaticMaxY + 3f;
			}
			boxMoveMinY = 0f - boxMoveMaxY;
			boxStaticMinY = 0f - boxStaticMaxY;
			float num = itemStartY;
			for (int i = 0; i < items.Count; i++)
			{
				CharacterItem characterItem = items[i];
				if (characterItem != null)
				{
					if (needUpdatePosition)
					{
						characterItem.SetLocalPosition(0f, num);
					}
					else if (characterItem == newItem)
					{
						characterItem.MoveToLocalPosition(new Vector2(0f, num));
					}
					else
					{
						characterItem.MoveToLocalPosition(new Vector2(0f, num));
					}
					num += 0.4f + characterItem.characterHeight;
				}
			}
		}

		public void SetFloorVisible(bool val)
		{
			floor.gameObject.SetActive(val);
		}

		public void SetFloorColliderEnabled(bool val)
		{
			floorColl.enabled = val;
		}

		public void AddItem(BaseItem item)
		{
			if (item.itemData.scriptType == 1)
			{
				if (items.Contains((CharacterItem)item))
				{
					UpdateStorageBoxItemPosition(false);
					return;
				}
				((CharacterItem)item).IsLock(lockMask.activeSelf);
				boxHeight += ((CharacterItem)item).characterHeight + 0.4f;
				items.Add((CharacterItem)item);
				SortItemsByY();
				UpdateStorageBoxItemPosition(false, item);
			}
		}

		private void SortItemsByY()
		{
			for (int i = 0; i < items.Count - 1; i++)
			{
				for (int num = items.Count - 1; num > i; num--)
				{
					if (items[num - 1].transform.position.y > items[num].transform.position.y)
					{
						CharacterItem value = items[num];
						items[num] = items[num - 1];
						items[num - 1] = value;
					}
				}
			}
		}

		public void RemoveItem(BaseItem item, bool needUpdateBox = true)
		{
			if (item.itemData.scriptType == 1 && items.Remove((CharacterItem)item))
			{
				boxHeight -= ((CharacterItem)item).characterHeight + 0.4f;
				if (needUpdateBox)
				{
					UpdateStorageBoxItemPosition(false);
				}
			}
		}

		public void SetBoxY(float val)
		{
			box.localPosition = new Vector3(box.localPosition.x, val, 0f);
		}

		public float GetBoxY()
		{
			return box.localPosition.y;
		}

		public float CalBoxMoveY(float val)
		{
			return Mathf.Clamp(val, boxMoveMinY, boxMoveMaxY);
		}

		public float CalBoxStaticY(float val)
		{
			return Mathf.Clamp(val, boxStaticMinY, boxStaticMaxY);
		}

		public void Clear()
		{
			if (storageBox != null)
			{
				Object.Destroy(storageBox.gameObject);
				storageBox = null;
			}
		}
	}
}
